<template>
	<view class="page" :style="themeColor">
		<view class="flex benben-position-layout flex flex-wrap align-center payImmediately_flex_0"
			:style="{height:(88+StatusBarRpx)+'rpx',paddingTop:StatusBarRpx+'rpx'}">
			<view class='flex flex-wrap align-center justify-between flex-sub payImmediately_fd0_0'>
				<view class='flex flex-wrap align-center' @tap.stop="handleJumpDiy" data-type="back" data-url="1">
					<text class='fu-iconfont2  payImmediately_fd0_0_c0_c0'>&#xE794;</text>
				</view>
				<view class='flex flex-wrap align-stretch justify-center flex-sub'>
					<text class='payImmediately_fd0_0_c1_c0'>充值</text>
				</view>
				<view class='flex flex-wrap align-center payImmediately_fd0_0_c2'>
				</view>
			</view>

		</view>
		<view :style="{height: (88+StatusBarRpx)+'rpx'}"></view>

		<view class="flex flex-direction flex-wrap align-stretch payImmediately_flex_1">
			<view class='flex flex-wrap align-center justify-between payImmediately_fd1_0'>
				<text class='payImmediately_fd1_0_c0'>选择支付方式</text>
			</view>
			<benben-select-diy ref="showSelectPopup1681356052929" class-name='flex flex-direction align-stretch flex'
				:items.sync="select_type" v-model="select_id" default-type="id" default-label="pay_way"
				:allow-cancel='false' type="radio" :disabled='false' @change="selectPay">
				<template v-for='(item,key0) in select_type'>
					<view v-if="item.isSelected" class='flex align-center flex payImmediately_fd1_1_c0' :key="key0"
						@tap="$refs.showSelectPopup1681356052929.tapHandle(key0)">
						<image class='payImmediately_fd1_1_c0_c0' mode="aspectFit" :src='item.thumb'></image>
						<text class='flex-sub payImmediately_fd1_1_c0_c1'>{{item.name}}</text>
						<image class='payImmediately_fd1_1_c0_c2' mode="aspectFit" :src='STATIC_URL+"1.png"'></image>
					</view>
					<view v-else class='flex align-center flex payImmediately_fd1_1_c0' :key="key0"
						@tap="$refs.showSelectPopup1681356052929.tapHandle(key0)">
						<image class='payImmediately_fd1_1_c0_c0' mode="aspectFit" :src='item.thumb'></image>
						<text class='flex-sub payImmediately_fd1_1_c0_c1'>{{item.name}}</text>
						<image class='payImmediately_fd1_1_c0_c2' mode="aspectFit" :src='STATIC_URL+"52.png"'></image>
					</view>
				</template></benben-select-diy>
		</view>


		<view class="flex flex-direction flex-wrap align-center benben-position-layout flex payImmediately_flex_2">
			<button class='payImmediately_fd2_0' @tap.stop="goRecharge">充值</button>

		</view>
		<view :style="{height: '152rpx'}"></view>


	</view>
</template>
<script>
	export default {
		components: {},


		data() {
			return {
				"select_id": "1",
				"pay_way": "", // 支付方式
				"select_type": [],
				"recharge_id": "", // 充值规则id
				"order_sn": "", // 订单号
				"pay_time": 0,
				"timer": null,
			};
		},
		computed: {
			themeColor() {
				return this.$store.getters.themeColor
			},

		},
		watch: {},
		onLoad(options) {
			let {
				recharge_id
			} = options
			if (recharge_id !== undefined) this.recharge_id = recharge_id
			this.getPayRules();
		},
		onUnload() {

		},
		onReady() {

		},
		onShow() {

		},
		onHide() {

		},
		onResize() {

		},
		onPullDownRefresh() {

		},
		onReachBottom(e) {

		},
		onPageScroll(e) {

		},
		methods: {
			getPayRules() {
				this.$api.post(global.API_VERSION + '/647d825640cdb', {

				}).then(res => {
					// console.log(res);
					if (res.data.code == 1) {
						this.select_type = res.data.data;
						if (this.select_type.length > 0) {
							this.select_id = this.select_type[0].id;
							this.pay_way = this.select_type[0].pay_way;
						}
					} else {
						this.$message.info(res.data.msg);
					}
				})
			},
			selectPay(e) {
				this.select_id = e.label;
				this.pay_way = e.value;
				if (this.pay_way == 'counter') {
					this.select_type.forEach((v, k) => {
						if (v.pay_way == 'counter') {
							global.bankInfo = v;
						}
					})
				} else if (this.pay_way == 'paypal') {

				} else if (this.pay_way == 'visa') {

				}
			},
			// 充值
			goRecharge() {
				if (this.pay_way == 'counter') {
					uni.redirectTo({
						url: '/pages/qbgl/remittance/remittance?recharge_id=' + this.recharge_id
					})
				} else if (this.pay_way == 'paypal') {
					this.submitPay();
				} else {
					this.$message.info('该充值方式未开启');
				}
			},
			submitPay() {
				this.$api.post(global.API_VERSION + '/647d826576b54', {
					recharge_id: this.recharge_id,
					pay_way: 'paypal'
				}).then(res => {
					// console.log(res);
					if (res.data.code == 1) {
						this.order_sn = res.data.data.order_sn;
						uni.showLoading({
							title: '加载中'
						})
						this.getPayPalInfo(res.data.data.order_sn);
					} else {
						this.$message.info(res.data.msg);
					}
				})
			},

			getPayPalInfo(order_sn) {
				let _this = this;
				_this.$api.post(global.apiUrls.post64a91ab6cdae4, {
					order_sn: order_sn,
					pay_type: _this.pay_way
				}).then(res => {
					// console.log(res);
					uni.hideLoading();
					uni.setStorageSync('paypalUrl', res.data.data.url);
					uni.redirectTo({
						url: '/pages/qbgl/payImmediately/paypal'
					})
					// plus.runtime.openURL(res.data.data.url);
					return;
					
					let orderInfo = {
						'clientId': res.data.data.clientId,
						'orderId': res.data.data.orderId,
						"userAction": "continue",  //  paynow/continue
						"currency": res.data.data.currency,
						'environment': res.data.data.environment
					};
					if (res.data.code == 1) {
						
						var paypalSev = null;
						plus.payment.getChannels(function(channels){
						    for (var i in channels) {
						        var channel = channels[i];
						        if (channel.id === 'paypal') {
						            paypalSev = channel;
						        }
						    }
						    //发起支付
						    plus.payment.request(paypalSev, orderInfo, function(result) {
						         var rawdata = JSON.parse(result.rawdata);
						         console.log("支付成功", rawdata);
						    }, function(e) {
						         console.log("支付失败：" + JSON.stringify(e));
						    });
						  }, function(e){
						      console.log("获取支付渠道失败：" + JSON.stringify(e));
						});
						return;
						
						
						
						
						
						uni.getProvider({
							service: 'payment',
							success: function(res) {
								console.log(res.provider)
								if (~res.provider.indexOf('paypal')) {
									uni.requestPayment({
										"provider": "paypal",
										"orderInfo": orderInfo,
										success: function(res) {
											uni.hideLoading();
											var rawdata = JSON.parse(res.rawdata);
											console.log("orderId：" + rawdata.orderId);
											if (_this.timer) {
												_this.timer = null;
											}
											_this.timer = setInterval(() => {
												if (_this.pay_time >= 600) {
													_this.pay_time = 0;
													_this.timer = null;
													uni.hideLoading();
													_this.postTimedCancel();
													clearInterval(_this.timer);
												}
												_this.getPayResult();
												_this.pay_time += 2;
												uni.showLoading({
													title: '等待支付结果...'
												})
											}, 2000);
										},
										fail: function(err) {
											uni.hideLoading();
											console.log('fail:' + JSON.stringify(err));
										}
									});
								} else {
									uni.hideLoading();
									_this.$message.info('暂不支持paypal支付');
								}
							}
						});
					} else {
						uni.hideLoading();
						_this.$message.info(res.data.msg);
					}
				})
			},
			// 定时器刷新支付结果
			getPayResult() {
				this.$api.post(global.apiUrls.post64a937222b800, {
					order_sn: this.order_sn
				}).then(res => {
					uni.hideLoading();
					if (res.data.code == 1) {
						if (res.data.data.is_pay == 1) {
							this.$message.success("支付成功");
							this.pay_time = 0;
							clearInterval(this.timer);
							this.pay_show = false;
							this.timer = null;
							setTimeout(() => {
								uni.redirectTo({
									url: '/pages/qbgl/successfullyRecharged/successfullyRecharged?type=1'
								})
							}, 800)
						}
					} else {
						uni.hideLoading();
						this.$message.info(res.data.msg);
					}
				})
			},
			// 取消订单
			postTimedCancel() {
				this.$api.post(global.apiUrls.post64a9373848294, {
					order_sn: this.order_sn
				}).then(res => {
					this.$message.info(res.data.msg);
				})
			},
		}
	};
</script>
<style lang="scss" scoped>
	.page {
		width: 100vw;
		overflow-x: hidden;
		min-height: calc(100vh - var(--window-bottom));
		background: #F8F8F8;
		background-size: 100% auto !important;
	}

	.payImmediately_flex_0 {
		background: #fff;
		width: 750rpx;
		height: 88rpx;
		overflow: hidden;
		z-index: 10;
		top: 0rpx;
		background-size: 100% auto !important;
	}

	.payImmediately_fd0_0_c2 {
		width: 32rpx;
		height: 32rpx;
	}

	.payImmediately_fd0_0_c1_c0 {
		font-size: 36rpx;
		font-weight: 500;
		color: #000000;
		line-height: 88rpx;
	}

	.payImmediately_fd0_0_c0_c0 {
		font-size: 32rpx;
		font-weight: 500;
		color: #333;
		line-height: 88rpx;
	}

	.payImmediately_fd0_0 {
		margin: 0rpx 32rpx 0rpx 32rpx;
	}

	.payImmediately_flex_1 {
		background: #fff;
		background-size: #eee;
		border-radius: 16rpx 16rpx 16rpx 16rpx;
		padding: 0rpx 32rpx 0rpx 32rpx;
	}

	.payImmediately_fd1_1_c0_c2 {
		width: 36rpx;
		height: 36rpx;
	}

	.payImmediately_fd1_1_c0_c1 {
		font-size: 32rpx;
		font-weight: 400;
		color: #333333;
		margin: 0rpx 24rpx 0rpx 24rpx;
	}

	.payImmediately_fd1_1_c0_c0 {
		width: 56rpx;
		height: 56rpx;
	}

	.payImmediately_fd1_1_c0 {
		padding: 32rpx 0rpx 32rpx 0rpx;
	}

	.payImmediately_fd1_0_c0 {
		font-weight: 400;
		font-size: 28rpx;
		color: #666666;
	}

	.payImmediately_fd1_0 {
		border-bottom: 1px solid #eee;
		line-height: 45rpx;
		margin: 0rpx 0rpx 8rpx 0rpx;
		padding: 32rpx 0rpx 32rpx 0rpx;
	}

	.payImmediately_flex_2 {
		width: 750rpx;
		height: 152rpx;
		overflow: hidden;
		z-index: 10;
		bottom: calc(0rpx + var(--window-bottom));
	}

	.payImmediately_fd2_0 {
		background: #6A0002;
		border-radius: 44rpx 44rpx 44rpx 44rpx;
		font-size: 32rpx;
		color: #fff;
		width: 638rpx;
		height: 88rpx;
		line-height: 88rpx;
	}
</style>